package crm.view.statistic;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import crm.model.teacherPC.statistic.Statistic;
import crm.model.teacherPC.statistic.StatisticValue;

public class StatisticTable implements TableModel {

	Object[] values;
	Statistic statistic;
	ArrayList<TableModelListener> listener = new ArrayList<TableModelListener>();
	StatisticTable(Statistic statistic){
		this.statistic = statistic;
		values = statistic.map.values().toArray();
	}

	private void fireListener() {
		for (TableModelListener l : listener) {
			l.tableChanged(new TableModelEvent(this));
		}

	}



	public Class<?> getColumnClass(int index) {
		switch (index){
		case 0:
			return String.class;
		case 1:
			return String.class;
		case 2:
			return Integer.class;
		}
		return null;
	}

	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return 3;
	}

	@Override
	public String getColumnName(int index) {
		switch (index){
		case 0:
			return "Name der Anwendung";
		case 1:
			return "Zeit, in der sie offen war";
		case 2:
			return "%";
		}
		return null;
	}

	@Override
	public int getRowCount() {

		return values.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex){
		case 0:
			return ((StatisticValue)(values[rowIndex])).getName();
		case 1:
			long ms = ((StatisticValue)(values[rowIndex])).getTime();
			Date date = new Date(ms);
			SimpleDateFormat d = new SimpleDateFormat("mm:ss");
			return d.format(date);
		case 2:
			return (((double)((StatisticValue)(values[rowIndex])).getTime()) / ((double)statistic.sumTime) ) * 100;
		}
		return null;
	}

	@Override
	public boolean isCellEditable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void removeTableModelListener(TableModelListener l) {
		listener.remove(l);

	}
	public void addTableModelListener(TableModelListener l) {
		listener.add(l);

	}
	@Override
	public void setValueAt(Object arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}
}
